package com.warehouse.module.business.controller.admin.productnametype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.framework.common.util.object.BeanUtils;
import static com.warehouse.framework.common.pojo.CommonResult.success;

import com.warehouse.framework.excel.core.util.ExcelUtils;

import com.warehouse.framework.apilog.core.annotation.ApiAccessLog;
import static com.warehouse.framework.apilog.core.enums.OperateTypeEnum.*;

import com.warehouse.module.business.controller.admin.productnametype.vo.*;
import com.warehouse.module.business.dal.dataobject.productnametype.ProductNameTypeDO;
import com.warehouse.module.business.service.productnametype.ProductNameTypeService;

@Tag(name = "管理后台 - 品名/型号管理")
@RestController
@RequestMapping("/business/product-name-type")
@Validated
public class ProductNameTypeController {

    @Resource
    private ProductNameTypeService productNameTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建品名/型号管理")
    @PreAuthorize("@ss.hasPermission('business:product-name-type:create')")
    public CommonResult<Long> createProductNameType(@Valid @RequestBody ProductNameTypeSaveReqVO createReqVO) {
        return success(productNameTypeService.createProductNameType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新品名/型号管理")
    @PreAuthorize("@ss.hasPermission('business:product-name-type:update')")
    public CommonResult<Boolean> updateProductNameType(@Valid @RequestBody ProductNameTypeSaveReqVO updateReqVO) {
        productNameTypeService.updateProductNameType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除品名/型号管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:product-name-type:delete')")
    public CommonResult<Boolean> deleteProductNameType(@RequestParam("id") Long id) {
        productNameTypeService.deleteProductNameType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得品名/型号管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:product-name-type:query')")
    public CommonResult<ProductNameTypeRespVO> getProductNameType(@RequestParam("id") Long id) {
        ProductNameTypeDO productNameType = productNameTypeService.getProductNameType(id);
        return success(BeanUtils.toBean(productNameType, ProductNameTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得品名/型号管理分页")
    @PreAuthorize("@ss.hasPermission('business:product-name-type:query')")
    public CommonResult<PageResult<ProductNameTypeRespVO>> getProductNameTypePage(@Valid ProductNameTypePageReqVO pageReqVO) {
        PageResult<ProductNameTypeDO> pageResult = productNameTypeService.getProductNameTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductNameTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出品名/型号管理 Excel")
    @PreAuthorize("@ss.hasPermission('business:product-name-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductNameTypeExcel(@Valid ProductNameTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductNameTypeDO> list = productNameTypeService.getProductNameTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "品名/型号管理.xls", "数据", ProductNameTypeRespVO.class,
                        BeanUtils.toBean(list, ProductNameTypeRespVO.class));
    }

}